package fr.neticar.webremoteclient.client;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LoginPanel extends Composite {

	private static final int DELAY_DELETE_ERROR_MESSAGE = 4000;

	private VerticalPanel container = new VerticalPanel();
	
	private StackPanel setUp = new StackPanel();

	private String host = "";

	private String port = "5900";

	private String password;

	private Grid grid;

	private TextBox hostTextBox;

	private TextBox portTextBox;

	private PasswordTextBox pwdTextBox;

	private Button connectButton;

	private Button disconnectButton;
	
	private Label errorMessage;

	public LoginPanel(final WRC vnc) {
		initWidget(this.container);
		grid = new Grid(4, 2);
		errorMessage = new Label();
		errorMessage.setStyleName("error");
		grid.setWidget(0, 0, new Label("Host : "));
		grid.setWidget(1, 0, new Label("Port : "));
		grid.setWidget(2, 0, new Label("Password : "));
		hostTextBox = new TextBox();
		hostTextBox.setText(this.host);
		portTextBox = new TextBox();
		portTextBox.setText(this.port);
		pwdTextBox = new PasswordTextBox();
		grid.setWidget(0, 1, hostTextBox);
		grid.setWidget(1, 1, portTextBox);
		grid.setWidget(2, 1, pwdTextBox);
		connectButton = new Button();
		disconnectButton = new Button();
		connectButton.setText("Connect");
		disconnectButton.setText("Disconnect");
		connectButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				host = hostTextBox.getText();
				port = portTextBox.getText();
				password = pwdTextBox.getText();
				vnc.startVncViewer(host, port, password);

			}
		});
		disconnectButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				vnc.stopVncViewer();
			}
		});
		this.setUp.add(grid, "Setup");
		this.container.add(setUp);
		this.container.add(errorMessage);
		switchToDisconnectedState();

	}

	public void switchToConnectedState() {
		setErrorMessage("");
		hostTextBox.setReadOnly(true);
		portTextBox.setReadOnly(true);
		pwdTextBox.setReadOnly(true);
		grid.setWidget(3, 1, disconnectButton);
	}

	public void switchToDisconnectedState() {
		hostTextBox.setReadOnly(false);
		portTextBox.setReadOnly(false);
		pwdTextBox.setReadOnly(false);
		grid.setWidget(3, 1, connectButton);
	}
	
	public void setErrorMessage(String message){
		errorMessage.setText(message);
		errorMessage.setVisible(true);
		Timer timer = new Timer(){
		
			public void run() {
				errorMessage.setText("");
				
				errorMessage.setVisible(false);
				
			}
		};
		timer.schedule(DELAY_DELETE_ERROR_MESSAGE);
	}

}
